//#extension GL_OES_EGL_image_external : require
precision mediump float;

varying vec2 textureCoordinate;

uniform sampler2D sTexture;

//highp vec4 params = vec4(0.6, 0.95, 0.03, 0.02);
//highp vec4 params = vec4(0.7, 0.7, 0.03, 0.02);
////highp vec4 params = vec4(0.5, 0.9, 0.01, 0.01);
highp vec4 params = vec4(0.61, 0.82, 0.25, 0.07);
const highp vec3 W = vec3(0.299,0.587,0.114);
const mat3 saturateMatrix = mat3(1.1102,-0.0598,-0.061,
                                 -0.0774,1.0826,-0.1186,
                                 -0.0228,-0.0228,1.1772);

float hardlight(float color) {
  if(color <= 0.5) {
     color = color * color * 2.0;
  } else {
     color = 1.0 - ((1.0 - color)*(1.0 - color) * 2.0);
   }
  return color;
}

void main() {
    vec4 tc = texture2D(sTexture, textureCoordinate);
    float color = tc.r * 0.3 + tc.g * 0.59 + tc.b * 0.11;
    vec2 blurCoordinates[24];
    vec2 singleStepOffset = vec2(1.0/360.0, 1.0/640.0);
    blurCoordinates[0] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -10.0);

    blurCoordinates[2] = textureCoordinate.xy + singleStepOffset * vec2(-10.0, 0.0);

    blurCoordinates[4] = textureCoordinate.xy + singleStepOffset * vec2(5.0, -8.0);
    
    blurCoordinates[6] = textureCoordinate.xy + singleStepOffset * vec2(-5.0, 8.0);
    

    blurCoordinates[8] = textureCoordinate.xy + singleStepOffset * vec2(8.0, -5.0);
    
    blurCoordinates[10] = textureCoordinate.xy + singleStepOffset * vec2(-8.0, 5.0);
    

    blurCoordinates[12] = textureCoordinate.xy + singleStepOffset * vec2(0.0, -6.0);
   
    blurCoordinates[14] = textureCoordinate.xy + singleStepOffset * vec2(6.0, 0.0);
    

    blurCoordinates[16] = textureCoordinate.xy + singleStepOffset * vec2(-4.0, -4.0);
   
    blurCoordinates[18] = textureCoordinate.xy + singleStepOffset * vec2(4.0, -4.0);
    

    blurCoordinates[20] = textureCoordinate.xy + singleStepOffset * vec2(-2.0, -2.0);
    
    blurCoordinates[22] = textureCoordinate.xy + singleStepOffset * vec2(2.0, -2.0);


    float sampleColor = tc.g * 11.0;
    sampleColor += texture2D(sTexture, blurCoordinates[0]).g;
   
    sampleColor += texture2D(sTexture, blurCoordinates[2]).g;
   
    sampleColor += texture2D(sTexture, blurCoordinates[4]).g;
   
    sampleColor += texture2D(sTexture, blurCoordinates[6]).g;
  
    sampleColor += texture2D(sTexture, blurCoordinates[8]).g;

    sampleColor += texture2D(sTexture, blurCoordinates[10]).g;

    sampleColor += texture2D(sTexture, blurCoordinates[12]).g * 2.0;

    sampleColor += texture2D(sTexture, blurCoordinates[14]).g * 2.0;

    sampleColor += texture2D(sTexture, blurCoordinates[16]).g * 2.0;

    sampleColor += texture2D(sTexture, blurCoordinates[18]).g * 2.0;

    sampleColor += texture2D(sTexture, blurCoordinates[20]).g * 3.0;

    sampleColor += texture2D(sTexture, blurCoordinates[22]).g * 3.0;

    sampleColor = sampleColor / 31.0;
    vec3 centralColor = tc.rgb;
    float highpass = centralColor.g - sampleColor + 0.5;
    for(int i = 0; i < 5;i++)
    {
       highpass = hardlight(highpass);
    }
    float lumance = dot(centralColor, W);
    float alpha = pow(lumance, params.r);
    vec3 smoothColor = centralColor + (centralColor-vec3(highpass))*alpha*0.07;
    
    gl_FragColor = vec4(smoothColor,1.0);
}